package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐和对应的菜品
     * @param setmealDTO
     */
    @Override
    @Transactional //添加事物注解 两张表操作setmeal和setmeal_dish
    public void addWithSetmealDishes(SetmealDTO setmealDTO) {
        //1、向套餐表插入数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //注意：获取insert语句生成的主键值 两个表通过id绑定
        Long setmealId = setmeal.getId();

        //2、向套餐菜品表插入n条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //拿到套餐菜品集合 判断再插入数据
        if(setmealDishes != null && !setmealDishes.isEmpty()){
            for (SetmealDish setmealDish : setmealDishes) {
                //遍历并赋值 传入setmeal_id
                setmealDish.setSetmealId(setmealId);
            }
            //批量插入n条菜品数据
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //使用mybatis的分页查询插件 page helper - 开始分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //VO是后端给前端显示界面的数据类型 DTO是后端获取前端的数据类型 POJO是后端和数据库对应的数据类型
        Page<SetmealVO> page =  setmealMapper.pageQuery(setmealPageQueryDTO);

        //返回pageResult类型
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询套餐信息 对应的菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithSetmealDishs(Long id) {
        //1、根据id查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);

        //2、根据id查询套餐菜品数据
        List<SetmealDish> setmealDishs = setmealDishMapper.getByIdWithSetmealDishs(id);

        //3、封装成SetmealVO返回
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishs);
        return setmealVO;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //1、判断当前套餐是否可以删除 -- 是否存在起售中的套餐？
        for (Long id : ids) {
            //根据id查询套餐数据 获得这个套餐的状态status
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE) {
                //起售中的套餐不能删除 -- 直接仍异常结束
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //2、删除套餐表中的数据 -- 批量删除 delete from setmeal where id in (1,2,3,4)
        setmealMapper.deleteByIds(ids);

        //3、删除套餐菜品表中的数据 -- 批量删除 delete from setmeal_dish where setmeal_id in (1,2,3,4)
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 修改套餐 关联的菜品
     * @param setmealDTO
     */
    @Override
    public void updateWithSetmealDish(SetmealDTO setmealDTO) {
        //1、修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //2、修改套餐菜品表
        //a、删除原有的菜品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());

        //b、添加新的菜品 -- 先判断再插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && !setmealDishes.isEmpty()){ //拿到菜品集合 判断再插入数据
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());   //遍历并赋值 传入setmeal_id
            }
            setmealDishMapper.insertBatch(setmealDishes);  //批量插入菜品数据
        }
    }

    /**
     * 套餐的起售停售
     * @param status
     * @param setmealId
     */
    @Override
    public void startOrStop(Integer status, Long setmealId) {
        //如果套餐是要起售的 需要进一步判断套餐关联的菜品是否都是起售的
        if(status == StatusConstant.ENABLE){
            //通过当前套餐id 查询关联的菜品信息
            List<SetmealDish> setmealDishs = setmealDishMapper.getByIdWithSetmealDishs(setmealId);
            setmealDishs.forEach(setmealDish ->{
                Long dishId = setmealDish.getDishId();
                Dish dish = dishMapper.getById(dishId);
                if(dish.getStatus() == StatusConstant.DISABLE){
                    //找到一个停售的菜品 就抛异常
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        //设置套餐起售
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(setmealId)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
